package com.film.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.film.dao.DirectorDao;
import com.film.domain.Director;
import com.film.domain.Film;
import com.film.rowMapper.DirectorRowMapper;
import com.film.rowMapper.FilmRowMapper;
import com.film.util.DBUtil;

public class DirectorDaoImpl implements DirectorDao {
	private JdbcTemplate temp = new JdbcTemplate(DBUtil.getDataScorse());
	@Override
	public List<Director> selectFilmDirector(int film_id) {
		String sql="select * from director t1,film_director t2 where t1.dir_id = t2.dir_id and t2.film_id = ?";
		return temp.query(sql, new DirectorRowMapper(),film_id);
	}
	
	
	@Override
	public Director selectOneDirector(int dirId) {
		String sql = "select * from director where dir_id = ?";
		List<Director> directors = temp.query(sql, new DirectorRowMapper(),dirId);
		if(directors!=null) {
			return directors.get(0);
		}else {
			return null;
		}
		
	}


	@Override
	public List<Film> dirIdSelectFilm(int dirId) {
		String sql = "select distinct t1.* from film t1 , film_director t2 ,director t3 where t1.film_id = t2.film_id and t2.dir_id = ?";
		return temp.query(sql, new FilmRowMapper(),dirId);
	}


	@Override
	public List<Director> selectDirectors() {
		String sql = "select * from director";
		return temp.query(sql, new DirectorRowMapper());
	}


	@Override
	public int addFilmDirector(int filmId, int dirId) {
		
		String sql = "insert into film_director values(default,?,?)";
		return temp.update(sql,filmId,dirId);
		
	}











	@Override
	public int deleteFilmDirector(int dirId) {
		String sql = "delete from director where dir_id= ?";
		return  temp.update(sql,dirId);
	}
	@Override
	public int addFilmDirector(Director director) {
		String sql="insert into director values(default,?,?,?,?,?,?,?,?,?,?,?)";
		return temp.update(sql,director.getDirName(),director.getDirNickName(),director.getDirTypr(),
				director.getDirBirth(),director.getDirHeight(),director.getDirSex(),
				director.getDirNationnality(),director.getDirBirthPlace(),
				director.getDirStar(),director.getDirIntroduce(),director.getDirImg());
	}
	@Override
	//UPDATE runoob_tbl SET runoob_title='学习 C++' WHERE runoob_id=3;
	public int updateFilmDirector(Director director) {
		String sql="update director set dir_name=?,dir_foreignName=?,dir_nickname=?,dir_birth=?,dir_height=?,"
				+ "dir_sex=?,dir_nationality=?,dir_birthPlace=?,dir_star=?,dir_introduce=? where dir_id=?";
		return temp.update(sql,director.getDirName(),director.getDirNickName(),director.getDirTypr(),
				director.getDirBirth(),director.getDirHeight(),director.getDirSex(),
				director.getDirNationnality(),director.getDirBirthPlace(),
				director.getDirStar(),director.getDirIntroduce(),director.getDirId());
	}
	@Override
	public List<Director> selectDirector() {
		String sql="select * from director";
		return  temp.query(sql, new DirectorRowMapper());	
	}
	
	@Override
	public Director selectDirectorById(int dirId) {
		String sql= "select * from director where dir_Id= ? ";
		return temp.query(sql, new DirectorRowMapper(),dirId).get(0);
	}
	@Override
	public List<Director> selectDirector(int firstPage, int selectPageNum) {
		String sql="select * from director limit ?,?";
		return temp.query(sql, new DirectorRowMapper(),firstPage,selectPageNum);
	}

}
